package books4africa

class InventoryException extends RuntimeException {
      
      String message
      String inv
      }

class InventoryService {

      def addToInventory(Donation donation, int bookQty) { 
		  
            def addInv = new Inventory([book : donation.book, donation : donation, qty: bookQty])
            
            if (!addInv.save())
            {
                  throw new InventoryException(message: "Unable to save to inventory", inv: addInv.errors.each {println it})
            }
    }
	  
	  def removeFromInventory(Donation donation, int bookQty){
		  
		  // get inventory record to decrease qty
		  def inv = Inventory.executeQuery("select inv from Inventory inv where donation = :donation", [donation:donation])
		  
		  int newQty = inv[0].qty - bookQty
		  
		  inv[0].qty = newQty
		  
		  inv[0].save()
	  }
}
